package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.UnqualifiedDisposalLine;
import com.mxpio.erp.common.quality.service.UnqualifiedDisposalLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "UnqualifiedDisposalLineController", description ="不良品处理单行接口" )
@RestController
@RequestMapping("/erp/quality/udline/")
public class UnqualifiedDisposalLineController {

    @Autowired
    private UnqualifiedDisposalLineService unqualifiedDisposalLineService;

    @GetMapping("list")
    @Operation(summary = "不良品处理单行列表", description = "获取不良品处理单行列表（无分页）", method = "GET")
    public Result<List<UnqualifiedDisposalLine>> list(@Parameter(name="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<UnqualifiedDisposalLine> lines = unqualifiedDisposalLineService.list(UnqualifiedDisposalLine.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "不良品处理单行列表", description = "获取不良品处理单行列表（分页）", method = "GET")
    public Result<Page<UnqualifiedDisposalLine>> page(@Parameter(name="查询参数") Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<UnqualifiedDisposalLine> pagelines = unqualifiedDisposalLineService.listPage(UnqualifiedDisposalLine.class,page, criteria);
        return Result.OK(pagelines);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取不良品处理单行", description = "根据bizNo获取不良品处理单行", method = "GET")
    public Result<List<UnqualifiedDisposalLine>> getByBizNo(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<UnqualifiedDisposalLine> list = unqualifiedDisposalLineService.listWith(c);
        if (list.size()>0){
            list.forEach(per -> per.setSaveTransient(false));//save时 不需要处理明细行上关联的主单对象
        }
        return Result.OK(list);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除不良品处理单行", description = "删除不良品处理单行,多个id用英文逗号分隔", method = "DELETE")
    public Result<UnqualifiedDisposalLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            unqualifiedDisposalLineService.remove(UnqualifiedDisposalLine.class, key);
        }
        return Result.OK();
    }
}

